package com.company.woye.taiorcheckSize;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.company.woye.MyApplication;
import com.company.woye.R;
import com.company.woye.base.BaseActivity;
import com.company.woye.checkmakesure.MakeSureDetailActivity;
import com.company.woye.checkmakesure.bean.MakeSureBean;
import com.company.woye.checkmakesure.bean.SuccessBean;
import com.company.woye.internet.KjjPHCallBack;
import com.company.woye.taiorcheckSize.adapter.TaiorPartDetailListviewAdapter;
import com.company.woye.taiorcheckSize.bean.TaiorPartBean;
import com.company.woye.utils.Constans;
import com.company.woye.utils.Toaster;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.MediaType;

public class TaiorCheckDetailActivity extends BaseActivity {

    @BindView(R.id.iv_on_package_back)
    ImageView ivOnPackageBack;
    @BindView(R.id.tv_on_package_back)
    TextView tvOnPackageBack;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    @BindView(R.id.tv_on_package_sure)
    TextView tvOnPackageSure;
    @BindView(R.id.tv_refresh)
    TextView tvRefresh;
    @BindView(R.id.tv_current_code)
    TextView tvCurrentCode;
    @BindView(R.id.tv_scan_code)
    TextView tvScanCode;
    @BindView(R.id.tv_DBH)
    TextView tvDBH;
    @BindView(R.id.tv_orderNo)
    TextView tvOrderNo;
    @BindView(R.id.tv_po)
    TextView tvPo;
    @BindView(R.id.tv_size)
    TextView tvSize;
    @BindView(R.id.tv_piece)
    TextView tvPiece;
    @BindView(R.id.tv_goodsNo)
    TextView tvGoodsNo;
    @BindView(R.id.tv_style)
    TextView tvStyle;
    @BindView(R.id.tv_sc)
    TextView tvSc;
    @BindView(R.id.tv_sh)
    TextView tvSh;
    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.lv_detail)
    ListView lvDetail;
    @BindView(R.id.ll_on_package_summary)
    LinearLayout llOnPackageSummary;
    @BindView(R.id.rl)
    RelativeLayout rl;
    TaiorPartBean.DataBean data;
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (receiver != null) {
            unregisterReceiver(receiver);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    TaiorPartDetailListviewAdapter adapter;
    List<TaiorPartBean.DataBean.YbccbListBean> listBeans = new ArrayList<>();

    @Override
    public int setLayoutId() {
        return R.layout.activity_taior_check_detail;
    }

    @Override
    public void initView() {
        /**
         * {
         *   "cjName": "裁剪一工场",
         *   "txm": "267000972635",
         *   "token": "7e0daaaed1a1543727122788a4e5398e7c23e88b075a32e3922a6d96b5642db9",
         *   "bmName": "制衣二部(1)"
         * }
         */
        IntentFilter inflater = new IntentFilter("finish");
        registerReceiver(receiver, inflater);
        String cjName = getIntent().getStringExtra("cjName");
        String txm = getIntent().getStringExtra("txm");
        String bmName = getIntent().getStringExtra("bmName");
        String lsx = getIntent().getStringExtra("lsx");
        String inputMan = getIntent().getStringExtra("inputMan");
        String inputId = getIntent().getStringExtra("inputId");
        String token = Constans.token;
        adapter = new TaiorPartDetailListviewAdapter(TaiorCheckDetailActivity.this, listBeans);
        lvDetail.setAdapter(adapter);
        getDetail(cjName, txm, bmName, token);

        lvDetail.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(TaiorCheckDetailActivity.this, ChangeSizeActivity.class);
                intent.putExtra("cjName", cjName);
                intent.putExtra("txm", txm);
                intent.putExtra("bmName", bmName);
                intent.putExtra("bw", listBeans.get(position).getBw());
                intent.putExtra("cm", data.getCm());
                intent.putExtra("hyh", data.getHyh());
                intent.putExtra("lsx", lsx);
                intent.putExtra("token", token);
                intent.putExtra("inputId", inputId);
                intent.putExtra("inputMan", inputMan);
                startActivity(intent);
            }
        });
    }

    private void getDetail(String cjName, String txm, String bmName, String token) {
        JSONObject params = new JSONObject();
        try {
            params.put("cjName", cjName);
            params.put("txm", txm);
            params.put("bmName", bmName);
            params.put("token", token);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("提交数据", params.toString());
        Log.e("访问地址", Constans.IP + "szapi/third/BdMlcj/listCjypmljy");
        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json"))
                .content(params.toString()).url(Constans.IP + "szapi/third/BdMlcj/listCjypmljy").build().execute(new KjjPHCallBack<TaiorPartBean>(TaiorPartBean.class, TaiorCheckDetailActivity.this, true) {
            @Override
            public void myError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(TaiorPartBean response, int id) {
                Toaster.showLong(TaiorCheckDetailActivity.this, response.getMsg());
                if (response.getMsg().equals("success")) {
                    //填充数据
                    data = response.getData();
                    tvCurrentCode.setText(data.getTxm());
                    tvScanCode.setText(data.getTxm());
                    tvDBH.setText(data.getDbh());
                    tvOrderNo.setText(data.getHyh());
                    tvPo.setText(data.getPoh());
                    tvSize.setText(data.getCm());
                    tvPiece.setText(data.getJs());
                    tvGoodsNo.setText(data.getHh());
                    tvStyle.setText(data.getCplx());
                    tvSc.setText(data.getSc());
                    tvSh.setText(data.getSh());
                    if (data.getYbccbList() != null && data.getYbccbList().size() > 0) {
                        listBeans.clear();
                        listBeans.addAll(data.getYbccbList());
                        adapter.notifyDataSetChanged();
                    }else {
                        listBeans.clear();
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    @OnClick({R.id.iv_on_package_back, R.id.tv_on_package_back, R.id.tv_refresh})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_on_package_back:
            case R.id.tv_on_package_back:
                finish();
                break;
            case R.id.tv_refresh:
                sendBroadcast(new Intent("finish"));
                finish();
                break;
        }
    }
}
